package com.cupk.controller;

import com.cupk.entity.Movie;
import com.cupk.entity.User;
import com.cupk.service.MovieService;
import com.cupk.service.UserService;
import com.cupk.util.StringTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author LY
 * @version 1.0
 */
@WebServlet("/Info")
public class InfoServlet extends HttpServlet {
    MovieService movieService = new MovieService();
    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        String userIdStr = req.getParameter("userId");
        Integer userId = Integer.parseInt(userIdStr);
        HttpSession session = req.getSession();
        User user = userService.findById(userId);
        if(method.equals("userInfo")){  //个人中心
            Integer[] collectible = null;
            if(user.getCollectible()!=null){    //收藏记录不为空
                collectible = StringTools.func2(user.getCollectible());
                List<Movie> favorites = movieService.findByIdGroup(collectible);
                req.setAttribute("collectible",favorites);
            }
        }else if(method.equals("deleteCollect")){   //删除收藏记录
            String movieIdStr = req.getParameter("movieId");
            Integer movieId = Integer.parseInt(movieIdStr);
            String collectibleStr = user.getCollectible();
            collectibleStr = collectibleStr.replaceAll("<" + movieIdStr + ">", "");
            userService.deleteCollect(collectibleStr,user.getId());
            if(collectibleStr.equals("")){
                req.setAttribute("collectible",null);
            }else {
                Integer[] collectible = StringTools.func2(collectibleStr);
                List<Movie> favorites = movieService.findByIdGroup(collectible);
                req.setAttribute("collectible",favorites);
            }

        }
            if(user.getRecords() != null){  //评分记录不为空
            HashMap<Integer,Integer> historyRate = StringTools.func(user.getRecords());
            ArrayList<Integer[]> records = StringTools.func1(user.getRecords());
            List<Movie> history = movieService.findByIdGroup(records.get(0));
            req.setAttribute("history",history);
            req.setAttribute("historyRate",historyRate);
        }
        session.setAttribute("user",user);
        req.getRequestDispatcher("userInfo.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        String password = req.getParameter("password");
        if(method.equals("register")){  //注册
            String username = req.getParameter("username");
            String hobbies = req.getParameter("hobbies");
            userService.register(username,password,hobbies);
            resp.sendRedirect("login.jsp");
        }else if(method.equals("editInformation")){ //个人首页修改信息
            String userIdStr = req.getParameter("userId");
            Integer userId = Integer.parseInt(userIdStr);
            String username = req.getParameter("title");
            String hobbies = req.getParameter("city");
            userService.editInformation(userId,username,password,hobbies);
            HttpSession session = req.getSession();
            User user = userService.findById(userId);
            List<Movie> tipList =movieService.findByType(user.getHobbies(),true);
            session.setAttribute("tipList",tipList);
            Integer[] collectible = null;
            if(user.getCollectible()!=null){    //收藏记录不为空
                collectible = StringTools.func2(user.getCollectible());
                List<Movie> favorites = movieService.findByIdGroup(collectible);
                req.setAttribute("collectible",favorites);
            }
            if(user.getRecords() != null){  //评分记录不为空
                HashMap<Integer,Integer> historyRate = StringTools.func(user.getRecords());
                ArrayList<Integer[]> records = StringTools.func1(user.getRecords());
                List<Movie> history = movieService.findByIdGroup(records.get(0));
                req.setAttribute("history",history);
                req.setAttribute("historyRate",historyRate);
            }

            session.setAttribute("user",user);
            req.getRequestDispatcher("userInfo.jsp").forward(req,resp);
        }
    }

}
